﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Yipusoft.Account.BLL;
using Yipusoft.Account.Model;
using System.Collections.Generic;

public partial class System_MenuNew : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindParentList("");
            BindSeqList();
            if (Request.QueryString["MenuID"] == null)
            {
                ActionTitle.Text = "添加菜单";
            }
            else
            {
                ActionTitle.Text = "编辑菜单";
                BindData();
                MenuID.ReadOnly = true;
            }
        }
    }
    private void BindParentList(string parentID)
    {
        ListItem li = new ListItem();
        if (parentID == "")
        {
            li.Text = "顶级菜单";
            li.Value = "";
            ParentList.Items.Add(li);
        }
        List<MenuInfo> menus = Yipusoft.Account.BLL.Menu.GetList("ParentID='" + parentID + "' ORDER BY SeqID");
        foreach (MenuInfo menu in menus)
        {
            li = new ListItem();
            li.Text = "".PadRight(2 * menu.MenuLevel, '　') + menu.MenuName + "(" + menu.MenuID + ")";
            li.Value = menu.MenuID;
            ParentList.Items.Add(li);
            BindParentList(menu.MenuID);
        }
    }
    private void BindSeqList()
    {
        ListItem li;
        for (int i = 0; i < 50;i++ )
        {
            li = new ListItem();
            li.Text = i.ToString();
            li.Value =i.ToString();
            SeqList.Items.Add(li);
        }
    }
    private void BindData()
    {
        MenuInfo menu = Yipusoft.Account.BLL.Menu.GetMenu(Request.QueryString["MenuID"]);
        MenuID.Text = menu.MenuID;
        ParentList.SelectedIndex = ParentList.Items.IndexOf(ParentList.Items.FindByValue(menu.ParentID));
        MenuName.Text = menu.MenuName;
        MenuUrl.Text = menu.MenuUrl;
        IsBlank.Checked = menu.IsBlank;
        MenuImage.Text = menu.MenuImage;
        SeqList.SelectedIndex = SeqList.Items.IndexOf(SeqList.Items.FindByValue(menu.SeqID.ToString()));
        Description.Text = menu.Description;
    }
    private void Insert()
    {
        if (Yipusoft.Account.BLL.Menu.Exsits(MenuID.Text))
        {
            Prompt.Text = "警告：该菜单编码已存在！";
            return;
        }
        MenuInfo menu = new MenuInfo();
        menu.MenuID = MenuID.Text;
        menu.ParentID = ParentList.SelectedValue;
        if (ParentList.SelectedValue != "")
            menu.MenuLevel = Yipusoft.Account.BLL.Menu.GetMenu(ParentList.SelectedValue).MenuLevel + 1;
        else
            menu.MenuLevel = 0;
        menu.MenuName = MenuName.Text;
        menu.MenuUrl = MenuUrl.Text;
        menu.IsBlank = IsBlank.Checked;
        menu.MenuImage = MenuImage.Text;
        menu.SeqID = int.Parse(SeqList.SelectedValue);
        menu.Description = Description.Text;
        Yipusoft.Account.BLL.Menu.Insert(menu);
        Prompt.Text = "提示：添加成功！";
    }
    private void Update()
    {
        MenuInfo menu = Yipusoft.Account.BLL.Menu.GetMenu(Request.QueryString["MenuID"]);
        menu.ParentID = ParentList.SelectedValue;
        if (ParentList.SelectedValue != "")
            menu.MenuLevel = Yipusoft.Account.BLL.Menu.GetMenu(ParentList.SelectedValue).MenuLevel + 1;
        else
            menu.MenuLevel = 0;
        menu.MenuName = MenuName.Text;
        menu.MenuUrl = MenuUrl.Text;
        menu.IsBlank = IsBlank.Checked;
        menu.MenuImage = MenuImage.Text;
        menu.SeqID = int.Parse(SeqList.SelectedValue);
        menu.Description = Description.Text;
        Yipusoft.Account.BLL.Menu.Update(menu);
        Prompt.Text = "提示：更新成功！";
    }
    protected void ButtonSave_Click(object sender, EventArgs e)
    {
        if (Request.QueryString["MenuID"] == null)
            Insert();
        else
            Update();
    }
}
